package com.app.newiptv.view;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.GridLayoutAnimationController;

public class MyRecycleView extends RecyclerView {
	private int position = 0;

	public MyRecycleView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		setChildrenDrawingOrderEnabled(true);
	}

	public MyRecycleView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setChildrenDrawingOrderEnabled(true);
	}

	public MyRecycleView(Context context) {
		super(context);
		setChildrenDrawingOrderEnabled(true);

	}

	@Override
	protected int getChildDrawingOrder(int childCount, int i) {
		View view = getFocusedChild();
		int foucsposition = getChildLayoutPosition(view);
		GridLayoutManager layoutManager = (GridLayoutManager) getLayoutManager();
		int firstPosition = layoutManager.findFirstVisibleItemPosition();
		position = foucsposition - firstPosition;
		if (position < 0) {
			return i;
		} else {
			if (i == childCount - 1) {// 这是最后一个需要刷新的item
				if (position > i) {
					position = i;
				}
				return position;
			}
			if (i == position) {// 这是原本要在最后一个刷新的item
				return childCount - 1;
			}
		}
		return i;
	}
	@Override
	protected void attachLayoutAnimationParameters(View child, ViewGroup.LayoutParams params, int index, int count) {

		if (getAdapter() != null && getLayoutManager() instanceof GridLayoutManager) {

			GridLayoutAnimationController.AnimationParameters animationParams =
					(GridLayoutAnimationController.AnimationParameters) params.layoutAnimationParameters;

			if (animationParams == null) {
				animationParams = new GridLayoutAnimationController.AnimationParameters();
				params.layoutAnimationParameters = animationParams;
			}

			int columns = ((GridLayoutManager) getLayoutManager()).getSpanCount();

			animationParams.count = count;
			animationParams.index = index;
			animationParams.columnsCount = columns;
			animationParams.rowsCount = count / columns;

			final int invertedIndex = count - 1 - index;
			animationParams.column = columns - 1 - (invertedIndex % columns);
			animationParams.row = animationParams.rowsCount - 1 - invertedIndex / columns;

		} else {
			super.attachLayoutAnimationParameters(child, params, index, count);
		}
	}

//	@Override
//	public void setLayoutManager(LayoutManager layout) {
//		if (layout instanceof GridLayoutManager){
//			super.setLayoutManager(layout);
//		} else {
//			throw new ClassCastException("You should only use a GridLayoutManager with GridRecyclerView.");
//		}
//	}
//
//	@Override
//	protected void attachLayoutAnimationParameters(View child, ViewGroup.LayoutParams params, int index, int count) {
//
//		if (getAdapter() != null && getLayoutManager() instanceof GridLayoutManager){
//
//			GridLayoutAnimationController.AnimationParameters animationParams =
//					(GridLayoutAnimationController.AnimationParameters) params.layoutAnimationParameters;
//
//			if (animationParams == null) {
//				animationParams = new GridLayoutAnimationController.AnimationParameters();
//				params.layoutAnimationParameters = animationParams;
//			}
//
//			int columns = ((GridLayoutManager) getLayoutManager()).getSpanCount();
//
//			animationParams.count = count;
//			animationParams.index = index;
//			animationParams.columnsCount = columns;
//			animationParams.rowsCount = count / columns;
//
//			final int invertedIndex = count - 1 - index;
//			animationParams.column = columns - 1 - (invertedIndex % columns);
//			animationParams.row = animationParams.rowsCount - 1 - invertedIndex / columns;
//
//		} else {
//			super.attachLayoutAnimationParameters(child, params, index, count);
//		}
//	}


}
